#include "volumetool.h"
#include "ui_volumetool.h"
#include <QGraphicsDropShadowEffect>
#include <QPainter>
VolumeTool::VolumeTool(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::VolumeTool),
    isMuted(false),
    volumeRatio(20)
{
    ui->setupUi(this);
    setWindowFlags(Qt::Popup | Qt::FramelessWindowHint| Qt::NoDropShadowWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
    QGraphicsDropShadowEffect* shadowEffect = new QGraphicsDropShadowEffect(this);
    shadowEffect->setOffset(0, 0);
    shadowEffect->setColor("#646464");
    shadowEffect->setBlurRadius(10);
    setGraphicsEffect(shadowEffect);
    ui->silenceBtn->setIcon(QIcon(":/images/volumn.png"));
    ui->outSlider->setGeometry(ui->outSlider->x(), ui->outSlider->y() + ui->outSlider->height() * 0.8, ui->outSlider->width(), 36);
    ui->silderBtn->move(ui->silderBtn->x(), ui->outSlider->y() - ui->silderBtn->height() / 2);
    ui->volumeRatio->setText("20%");
    connect(ui->silenceBtn,&QPushButton::clicked,this,&VolumeTool::onSilenceBtnClicked);
    ui->silderBox->installEventFilter(this);
}

VolumeTool::~VolumeTool()
{
    delete ui;
}

void VolumeTool::paintEvent(QPaintEvent *event)
{
    (void)event;
    // 1. 创建绘图对象
    QPainter painter(this);
    // 2. 设置抗锯⻮
    painter.setRenderHint(QPainter::Antialiasing, true);
    // 3. 设置画笔
    // 没有画笔时：画出来的图形就没有边框和轮廓线
    painter.setPen(Qt::NoPen);
    // 4. 设置画刷颜⾊
    painter.setBrush(Qt::white);
    // 创建⼀个三⻆形
    QPolygon polygon;
    polygon.append(QPoint(30, 300));
    polygon.append(QPoint(70, 300));
    polygon.append(QPoint(50, 320));
    // 绘制三⻆形
    painter.drawPolygon(polygon);
}

void VolumeTool::onSilenceBtnClicked()
{
    isMuted = !isMuted;
    if(isMuted)
    {
        ui->silenceBtn->setIcon(QIcon(":/images/silent.png"));
    }
    else
    {
        ui->silenceBtn->setIcon(QIcon(":/images/volumn.png"));
    }
    emit setSilence(isMuted);
}

bool VolumeTool::eventFilter(QObject *object, QEvent *event)
{
    if(object == ui->silderBox)
    {
        if(event->type() == QEvent::MouseButtonPress)
        {
            setVolume();
        }
        else if(event->type() == QEvent::MouseButtonRelease)
        {
            emit setMusicVolume(volumeRatio);
        }
        else if(event->type() == QEvent::MouseMove)
        {
            setVolume();
            emit setMusicVolume(volumeRatio);
        }
        return true;
    }
    return QObject::eventFilter(object,event);
}

void VolumeTool::setVolume()
{
    // 1. 将⿏标的位置转换为sloderBox上的相对坐标，此处只要获取y坐标
    int height = ui->silderBox->mapFromGlobal(QCursor().pos()).y();
    // 2. ⿏标在volumeBox中可移动的y范围在[25, 205之间]
    height = height < 25? 25 : height;
    height = height > 205? 205 : height;
    // 3. 调整sloderBt的位置
    ui->silderBtn->move(ui->silderBtn->x(), height - ui->silderBtn->height()/2);
    // 4. 更新outline的位置和⼤⼩
    ui->outSlider->setGeometry(ui->outSlider->x(), height, ui->outSlider->width(),205 - height);
    // 5. 计算⾳量⽐率
    volumeRatio = (int)((int)ui->outSlider->height()/(float)180*100);
    // 6. 设置给label显⽰出来
    ui->volumeRatio->setText(QString::number(volumeRatio)+"%");
}
